package nodeList;

import org.w3c.dom.NodeList;

import java.util.List;
import java.util.Stack;

/**
 * @author le
 */
public class ListLength
{
    public static void main(String[] args)
    {
        ListNode l1 = new ListNode(1);
        l1.add(2);
        l1.add(3);
        l1.print();

        ListNode l2 = new ListNode();

        Stack<Integer> stack = new Stack<>();
        while(l1 != null){
            stack.push(l1.val);
            l1 = l1.next;
        }
        for (Integer s : stack)
        {
            System.out.println(s);
        }
        ListNode l3 = new ListNode();
        l3 = l2;
        while(stack.size() != 0){
            l2.add(stack.pop());
        }
        l2.print();


        /*ListNode curr = l1;
        int flag = 0;
        System.out.println(curr.val);
        while(curr.next != null){
            if (flag == 0){
                l2.next = curr;
                curr = curr.next;
                flag++;
            }else{
                curr.next = l2.next;
                l2.next = curr;
                curr = curr.next;
            }
        }
        System.out.println(l2.next.val);*/
    }
}


class ListNode{
    int val;
    ListNode next;

    ListNode(int x){
        val = x;
    }

    ListNode(){

    }

    public void add(int newVal) {
        ListNode newNode = new ListNode(newVal);
        if(this.next == null)
        {
            this.next = newNode;
        }
        else{
            this.next.add(newVal);
        }

    }

    public void print() {
        System.out.print(this.val);
        if(this.next != null)
        {
            System.out.print("-->");
            this.next.print();
        }
    }
}
